package com.ljl.basic.controller;

import com.ljl.basic.exception.BusinessException;
import com.ljl.basic.service.IVerifyCodeSerive;
import com.ljl.basic.util.AjaxResult;
import com.ljl.org.dto.SmsCodeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verify")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeSerive verifyCodeSerive;

    @RequestMapping("/image/{key}")
    public AjaxResult image(@PathVariable("key") String key){
        try {
            String base64Str = verifyCodeSerive.getImageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取图形验证码失败！！");
        }
    }

    @PostMapping("/smsCode")
    public  AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeSerive.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }

    }
    @PostMapping("/binderSmsCode")
    public  AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            verifyCodeSerive.binderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("发送失败!"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常,请稍后重试!");
        }

    }

}
